import { InputItem } from '@/components';
import UserInfoController from '@/services/UserInfoService/UserInfoController';
import { Input, message,Modal } from 'antd';
import React from 'react';
const {Password} = Input
const {confirm} = Modal
class ChangePassWord extends React.Component {
    constructor(props) {
        super(props);
        this.state = { 
            datas:{}
         };
    }
    componentDidMount(){
        this.props.onRef(this)
    }
    handleChange=(type,e)=>{
        let {datas}=this.state
        switch(type){
            case 1:
                datas.password=e.target.value
                break;
            case 2:
                datas.again=e.target.value
                break;

        }
        this.setState({datas})
        console.log(type,e.target.value)
    }
    submit=()=>{
        let {datas}=this.state
        let userInfo = JSON.parse(sessionStorage.getItem('userInfo'))
        let that = this
        if(!datas.password){
            message.error('密码不能为空')
            return
        }
        if(datas.password.length<6){
            message.error('密码不能少于6位')
            return
        }
        datas.id=userInfo.id
        confirm({
            title:'确认',
            content:'确定要提交吗？',
            onOk(){
                UserInfoController.changePassword(datas).then(r=>{
                    if(r&&r.status===200&&r.data===1){
                        message.success('修改成功')
                        that.props.onCancel()
                    }
                })
            }
        })
        
    }
    render() {
        const {datas}=this.state
        return (
            <div>
                <Password placeholder={'请输入新密码'} onChange={this.handleChange.bind(this,1)} style={{margin:5}}/>
                <Password placeholder={'请再次输入'} onChange={this.handleChange.bind(this,2)} style={{margin:5}}/>
                {datas.password!==datas.again&&<span style={{color:'red'}}>两次密码不一致</span>}
                
            </div>
        );
    }
}

export default ChangePassWord;